异常setDefaultAuthenticator导致java错误。安全GWT web应用程序中的AccessControlException
我正在使用GWT创建一个web应用程序,服务器向客户机提供服务。在该服务中,它必须向另一台具有身份验证的服务器生成HTTP请求。以下代码描述了如何发出请求:
public int request()
{
Authenticator.setDefault(new MyAuthenticator(username, password));
try {
URL url = new URL(url_string);
URLConnection conn = url.openConnection();
conn.setRequestProperty("Content-Type", "text/xml");
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(buildRequest());
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//the rest is to handle the response ...
} catch (Exception e) {
//handle exception
}
}
MyAuthenticator
是按照建议的方式实现的here,我测试了代码,它工作起来很有魅力,但当我尝试将其纳入GWT项目时,我遇到了一个异常:
//huge stack trace above ... what is important is the following
Caused by: java.security.AccessControlException: access denied (java.net.NetPermission setDefaultAuthenticator)
有什么办法解决这个问题吗
# 1 楼答案
我可以看到两种情况:
# 2 楼答案
显然,GAE不允许验证器,这就是我得到这个异常的原因,发现了一个类似的问题here,解决方案是按照建议here手动添加认证头